#include <QMessageBox>
#include "mainscene.h"
#include "menu.h"
#include "config.h"
#include"bossplane.h"
#include <QPainter>
#include <ctime>
#include <QKeyEvent>
#include <QSound>
#include <QIcon>
#include <QPointer>
MainScene::MainScene(QWidget *parent)
    : QWidget(parent),
      m_isCoopMode(false),
      m_gameOver(false),
      m_recorder(0),
      m_scoreLabel(nullptr),
      m_highScoreLabel(nullptr)
{
    // 先初始化所有成员变量
    for(int i = 0; i < 5; i++) {
        m_keyPressed[i] = false;
        m_keyPressed2[i] = false;
    }
    m_scores[0] = m_scores[1] = 0;

    // 然后初始化场景
    initScene();
    playGame();
    // 确保玩家初始化
        m_hero1 = HeroPlane(true);
        m_hero2 = HeroPlane(false);
        m_hero1.m_Free = false;
        m_hero2.m_Free = true;
}
MainScene::~MainScene() {}

void MainScene::initScene()
{

    //设置窗口固定大小
    setFixedSize(GAME_WIDTH, GAME_HEIGHT);

    //设置窗口标题
    setWindowTitle(GAME_TITLE);

    //设置图标资源
    setWindowIcon(QIcon(GAME_ICON));

    //定时器间隔
    m_Timer.setInterval(GAME_RATE);

    m_gameOver = false;

    //敌机出场间隔初始化
    m_recorder = 0;

    //随机数种子
    srand((unsigned int)time(NULL));

    //初始化键盘状态
    for(int i = 0; i < 5; i++) {
        m_keyPressed[i] = false;
        m_keyPressed2[i] = false;
    }
    // 设置背景音乐
        m_backgroundSound.setSource(QUrl::fromLocalFile(":/res/bg.wav"));
        m_backgroundSound.setLoopCount(QSoundEffect::Infinite); // 无限循环
        m_backgroundSound.setVolume(0.5f); // 设置音量



        //得分
        // 创建得分标签 (右上角)
           m_scoreLabel = new QLabel(this);
           m_scoreLabel->setStyleSheet("QLabel { color: white; font: bold 14px; background-color: rgba(0,0,0,0.5); padding: 2px; }");
           m_scoreLabel->setAlignment(Qt::AlignRight | Qt::AlignTop);
           m_scoreLabel->setGeometry(width()-150, 10, 140, 40);
           m_scoreLabel->setText("得分: 0");

           // 创建历史最高分标签
           m_highScoreLabel = new QLabel(this);
           m_highScoreLabel->setStyleSheet("QLabel { color: yellow; font: bold 12px; background-color: rgba(0,0,0,0.5); padding: 2px; }");
           m_highScoreLabel->setAlignment(Qt::AlignRight | Qt::AlignTop);
           m_highScoreLabel->setGeometry(width()-150, 50, 140, 30);


}

void MainScene::playGame()
{
    m_Timer.stop();
    m_Timer.setInterval(GAME_RATE);

    disconnect(&m_Timer, &QTimer::timeout, 0, 0);

       connect(&m_Timer, &QTimer::timeout, this, [this](){
           if(!m_gameOver) {
               enemyToScene();
               updatePosition();
               collisionDetection();
               collisionDetection_Prop();
               update();
           }
       }, Qt::DirectConnection);  // 关键！使用直接连接

    m_Timer.start();
    m_backgroundSound.play();

}
void MainScene::updatePosition()
{
    // 更新地图坐标
    m_map.mapPosition();

    // 玩家1移动控制
    int x1 = m_hero1.m_X;
    int y1 = m_hero1.m_Y;
    if(m_keyPressed[0]) y1 -= HERO_MOVE_SPEED; // W
    if(m_keyPressed[1]) y1 += HERO_MOVE_SPEED; // S
    if(m_keyPressed[2]) x1 -= HERO_MOVE_SPEED; // A
    if(m_keyPressed[3]) x1 += HERO_MOVE_SPEED; // D

    // 玩家2移动控制
    int x2 = m_hero2.m_X;
    int y2 = m_hero2.m_Y;
    if(m_keyPressed2[0]) y2 -= HERO_MOVE_SPEED; // 上
    if(m_keyPressed2[1]) y2 += HERO_MOVE_SPEED; // 下
    if(m_keyPressed2[2]) x2 -= HERO_MOVE_SPEED; // 左
    if(m_keyPressed2[3]) x2 += HERO_MOVE_SPEED; // 右

    // 边界检查
    x1 = qBound(0, x1, GAME_WIDTH - m_hero1.m_Plane.width());
    y1 = qBound(0, y1, GAME_HEIGHT - m_hero1.m_Plane.height());
    x2 = qBound(0, x2, GAME_WIDTH - m_hero2.m_Plane.width());
    y2 = qBound(0, y2, GAME_HEIGHT - m_hero2.m_Plane.height());

    // 更新位置
    if(!m_hero1.m_Free) {
        m_hero1.setPosition(x1, y1);
    }
    if(!m_hero2.m_Free) {
        m_hero2.setPosition(x2, y2);
    }


    // 玩家1射击
    if(m_keyPressed[4]) {
        m_hero1.shoot();
    }

    // 玩家2射击
    if(m_keyPressed2[4]) {
        m_hero2.shoot();
    }

    // 计算子弹坐标
    for(int i = 0; i < BULLET_NUM; i++) {
        // 玩家1子弹
        if(!m_hero1.m_bullets[i].m_Free) {
            m_hero1.m_bullets[i].updatePostion();
        }
        // 玩家2子弹
        if(!m_hero2.m_bullets[i].m_Free) {
            m_hero2.m_bullets[i].updatePostion();
        }
    }

    // 计算敌机坐标
    for(int i = 0; i < ENEMY_NUM; i++) {
        if(!m_enemys[i].m_Free) {
            m_enemys[i].updatePosition();
        }
    }

    // 计算爆炸播放的图片
    for(int i = 0; i < BOMB_NUM; i++) {
        if(!m_bombs[i].m_Free) {
            m_bombs[i].updateInfo();
        }
    }
    //道具出场
       for(int i=0; i<PROP_NUM;i++)
       {
           if(m_props[i].m_Free==false)
           {
               m_props[i].updatePosition();
           }
       }

       //boss机出场
           if(m_bossplane.m_boss_Free == false){
               m_bossplane.updatePosition();
               //boss机发射子弹
               m_bossplane.shoot();
               for(int i=0 ;i<BULLET_NUM;i++)
               {
                   if(m_bossplane.m_boss_bullet[i].m_boss_Free == false)
                   {
                       m_bossplane.m_boss_bullet[i].update_boss_Position();
                   }
               }
           }

}

void MainScene::enemyToScene()
{
    m_recorder++;
    if(m_recorder < ENEMY_INTERVAL) {
        return;
    }

    m_recorder = 0;

    for(int i = 0; i < ENEMY_NUM; i++) {
        if(m_enemys[i].m_Free) {
            m_enemys[i].m_Free = false;
            m_enemys[i].m_X = rand() % (GAME_WIDTH - m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
            break;
        }
    }
}

void MainScene::collisionDetection()
{
    // 遍历所有非空闲的敌机
    for(int i = 0; i < ENEMY_NUM; i++) {
        if(m_enemys[i].m_Free) {
            continue;
        }

        // 检测玩家1子弹碰撞
        for(int j = 0; j < BULLET_NUM; j++) {
            if(!m_hero1.m_bullets[j].m_Free &&
               m_enemys[i].m_Rect.intersects(m_hero1.m_bullets[j].m_Rect))
            {
                // 敌机和子弹都消失
                m_enemys[i].m_Free = true;
                m_hero1.m_bullets[j].m_Free = true;

                updateScore(0, 5); // 玩家1加5分
                //判断boss机是否出场
                checkBossAppear();
                createProp(m_enemys[i].m_X, m_enemys[i].m_Y);

                //播放爆炸效果
                for(int k = 0; k < BOMB_NUM; k++) {
                    if(m_bombs[k].m_Free) {
                        m_bombs[k].m_Free = false;
                        m_bombs[k].m_X = m_enemys[i].m_X;
                        m_bombs[k].m_Y = m_enemys[i].m_Y;
                        break;
                    }
                }

                //播放爆炸音效
                QSound::play(SOUND_BOMB);

                // 立即触发重绘
                update();
            }
        }

        // 检测玩家2子弹碰撞
        for(int j = 0; j < BULLET_NUM; j++) {
            if(!m_hero2.m_bullets[j].m_Free &&
               m_enemys[i].m_Rect.intersects(m_hero2.m_bullets[j].m_Rect)) {
                // 敌机和子弹都消失
                m_enemys[i].m_Free = true;
                m_hero2.m_bullets[j].m_Free = true;

                updateScore(1, 5); // 玩家2加5分
                //判断boss机是否出场
                checkBossAppear();
                createProp(m_enemys[i].m_X, m_enemys[i].m_Y);

                //播放爆炸效果
                for(int k = 0; k < BOMB_NUM; k++) {
                    if(m_bombs[k].m_Free) {
                        m_bombs[k].m_Free = false;
                        m_bombs[k].m_X = m_enemys[i].m_X;
                        m_bombs[k].m_Y = m_enemys[i].m_Y;
                        break;
                    }
                }

                //播放爆炸音效
                QSound::play(SOUND_BOMB);

                // 立即触发重绘
                update();
            }
        }

        // 检测玩家1飞机与敌机碰撞
        if(m_enemys[i].m_Rect.intersects(m_hero1.m_Rect) && !m_hero1.m_Free) {
            // 敌机和玩家1飞机都消失
            m_enemys[i].m_Free = true;
            m_hero1.m_Free = true;

            //播放爆炸效果
            for(int k = 0; k < BOMB_NUM; k++) {
                if(m_bombs[k].m_Free) {
                    m_bombs[k].m_Free = false;
                    m_bombs[k].m_X = m_enemys[i].m_X;
                    m_bombs[k].m_Y = m_enemys[i].m_Y;
                    break;
                }
            }

            //播放爆炸音效
            QSound::play(SOUND_BOMB);
            if(!m_isCoopMode || (m_isCoopMode && m_hero2.m_Free)) {
                handleGameOver();
            }
        }

        // 检测玩家2飞机与敌机碰撞
        if(m_enemys[i].m_Rect.intersects(m_hero2.m_Rect) && !m_hero2.m_Free) {
            // 敌机和玩家2飞机都消失
            m_enemys[i].m_Free = true;
            m_hero2.m_Free = true;

            //播放爆炸效果
            for(int k = 0; k < BOMB_NUM; k++) {
                if(m_bombs[k].m_Free) {
                    m_bombs[k].m_Free = false;
                    m_bombs[k].m_X = m_enemys[i].m_X;
                    m_bombs[k].m_Y = m_enemys[i].m_Y;
                    break;
                }
            }

            //播放爆炸音效
            QSound::play(SOUND_BOMB);
            if(m_isCoopMode && m_hero1.m_Free) {
                handleGameOver();
            }
        }
    }

    // 检测玩家子弹与Boss机的碰撞
    if(!m_bossplane.m_boss_Free) // 只有当Boss机存在时才检测
    {
        // 玩家1子弹与Boss机碰撞
        for(int j = 0; j < BULLET_NUM; j++) {
            if(!m_hero1.m_bullets[j].m_Free &&
               m_bossplane.m_Rect.intersects(m_hero1.m_bullets[j].m_Rect))
            {
                m_hero1.m_bullets[j].m_Free = true; // 子弹消失
                m_bossplane.m_hp--; // Boss机减少生命值

                // 播放击中音效
                QSound::play(SOUND_BOMB);

                // Boss机被击败
                if(m_bossplane.m_hp <= 0) {
                    m_bossplane.m_boss_Free = true;
                    updateScore(0, 50); // 玩家1加50分
                    if(m_isCoopMode) {
                        updateScore(1, 50); // 合作模式下玩家2也加50分
                    }
                    for(int n=0;n<BULLET_NUM;n++){
                        m_bossplane.m_boss_bullet[n].m_boss_Free = true;
                    }

                    //播放爆炸效果
                    for(int k = 0; k < BOMB_NUM; k++) {
                        if(m_bombs[k].m_Free) {
                            m_bombs[k].m_Free = false;
                            m_bombs[k].m_X = m_bossplane.m_X;
                            m_bombs[k].m_Y = m_bossplane.m_Y;
                            break;
                        }
                    }

                    // 生成道具
                    createProp(m_bossplane.m_X, m_bossplane.m_Y);
                }

                // 立即触发重绘
                update();
            }
        }

        // 玩家2子弹与Boss机碰撞
        for(int j = 0; j < BULLET_NUM; j++) {
            if(!m_hero2.m_bullets[j].m_Free &&
               m_bossplane.m_Rect.intersects(m_hero2.m_bullets[j].m_Rect))
            {
                m_hero2.m_bullets[j].m_Free = true; // 子弹消失
                m_bossplane.m_hp--; // Boss机减少生命值

                // 播放击中音效
                QSound::play(SOUND_BOMB);

                // Boss机被击败
                if(m_bossplane.m_hp <= 0) {
                    m_bossplane.m_boss_Free = true;
                    updateScore(1, 50); // 玩家2加50分
                    if(m_isCoopMode) {
                        updateScore(0, 50); // 合作模式下玩家1也加50分
                    }
                    for(int n=0;n<BULLET_NUM;n++){
                        m_bossplane.m_boss_bullet[n].m_boss_Free = true;
                    }

                    //播放爆炸效果
                    for(int k = 0; k < BOMB_NUM; k++) {
                        if(m_bombs[k].m_Free) {
                            m_bombs[k].m_Free = false;
                            m_bombs[k].m_X = m_bossplane.m_X;
                            m_bombs[k].m_Y = m_bossplane.m_Y;
                            break;
                        }
                    }

                    // 生成道具
                    createProp(m_bossplane.m_X, m_bossplane.m_Y);
                }

                // 立即触发重绘
                update();
            }
        }
    }

    // 检测玩家飞机与boss子弹碰撞
    for(int j = 0; j < BULLET_NUM; j++) {
        // 玩家1与boss子弹碰撞
        if(!m_bossplane.m_boss_bullet[j].m_boss_Free &&
           m_bossplane.m_boss_bullet[j].m_boss_Rect.intersects(m_hero1.m_Rect) &&
           !m_hero1.m_Free)
        {
            m_bossplane.m_boss_bullet[j].m_boss_Free = true;
            m_hero1.m_Free = true;

            //播放爆炸效果
            for(int k = 0; k < BOMB_NUM; k++) {
                if(m_bombs[k].m_Free) {
                    m_bombs[k].m_Free = false;
                    m_bombs[k].m_X = m_hero1.m_X;
                    m_bombs[k].m_Y = m_hero1.m_Y;
                    break;
                }
            }

            //播放爆炸音效
            QSound::play(SOUND_BOMB);
            if(!m_isCoopMode || (m_isCoopMode && m_hero2.m_Free)) {
                handleGameOver();
            }
        }

        // 玩家2与boss子弹碰撞
        if(!m_bossplane.m_boss_bullet[j].m_boss_Free &&
           m_bossplane.m_boss_bullet[j].m_boss_Rect.intersects(m_hero2.m_Rect) &&
           !m_hero2.m_Free)
        {
            m_bossplane.m_boss_bullet[j].m_boss_Free = true;
            m_hero2.m_Free = true;

            //播放爆炸效果
            for(int k = 0; k < BOMB_NUM; k++) {
                if(m_bombs[k].m_Free) {
                    m_bombs[k].m_Free = false;
                    m_bombs[k].m_X = m_hero2.m_X;
                    m_bombs[k].m_Y = m_hero2.m_Y;
                    break;
                }
            }

            //播放爆炸音效
            QSound::play(SOUND_BOMB);
            if(m_isCoopMode && m_hero1.m_Free) {
                handleGameOver();
            }
        }
    }
}

void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0, m_map.m_map1_posY, m_map.m_map1);
    painter.drawPixmap(0, m_map.m_map2_posY, m_map.m_map2);

    //绘制玩家1

    if(!m_hero1.m_Free) {
           painter.drawPixmap(m_hero1.m_X, m_hero1.m_Y, m_hero1.m_Plane);
       }
    //绘制玩家2
    // 绘制玩家2（如果未被摧毁）
        if(!m_hero2.m_Free) {
            painter.drawPixmap(m_hero2.m_X, m_hero2.m_Y, m_hero2.m_Plane);
        }
    //绘制玩家1子弹
    for(int i = 0; i < BULLET_NUM; i++) {
        if(!m_hero1.m_bullets[i].m_Free) {
            painter.drawPixmap(m_hero1.m_bullets[i].m_X,
                             m_hero1.m_bullets[i].m_Y,
                             m_hero1.m_bullets[i].m_Bullet);
        }
    }

    //绘制玩家2子弹
    for(int i = 0; i < BULLET_NUM; i++) {
        if(!m_hero2.m_bullets[i].m_Free) {
            painter.drawPixmap(m_hero2.m_bullets[i].m_X,
                             m_hero2.m_bullets[i].m_Y,
                             m_hero2.m_bullets[i].m_Bullet);
        }
    }

    //绘制敌机
    for(int i = 0; i < ENEMY_NUM; i++) {
        if(!m_enemys[i].m_Free) {
            painter.drawPixmap(m_enemys[i].m_X, m_enemys[i].m_Y, m_enemys[i].m_enemy);
        }
    }

    //绘制爆炸
    for(int i = 0; i < BOMB_NUM; i++) {
        if(!m_bombs[i].m_Free) {
            painter.drawPixmap(m_bombs[i].m_X, m_bombs[i].m_Y,
                             m_bombs[i].m_pixArr[m_bombs[i].m_index]);
        }
    }
    //绘制道具
       for(int i =0;i<PROP_NUM;i++)
       {
           if(m_props[i].m_Free == false)
           {
               painter.drawPixmap(m_props[i].m_X,m_props[i].m_Y,m_props[i].m_prop);
           }
       }
       //绘制boss机
           if(m_bossplane.m_boss_Free == false)
           painter.drawPixmap(m_bossplane.m_X,m_bossplane.m_Y,m_bossplane.m_boss);

           //绘制boss机子弹
               for(int i =0;i<BULLET_NUM;i++)
               {
                   if(m_bossplane.m_boss_bullet[i].m_boss_Free == false)
                   {
                       painter.drawPixmap(m_bossplane.m_boss_bullet[i].m_boss_X,m_bossplane.m_boss_bullet[i].m_boss_Y,m_bossplane.m_boss_bullet[i].m_boss_Bullet);
                   }
               }

}





void MainScene::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()) {
        // 玩家1控制 (WSAD + 空格)
        case Qt::Key_W: m_keyPressed[0] = true; break;
        case Qt::Key_S: m_keyPressed[1] = true; break;
        case Qt::Key_A: m_keyPressed[2] = true; break;
        case Qt::Key_D: m_keyPressed[3] = true; break;
        case Qt::Key_Space: m_keyPressed[4] = true; break;

        // 玩家2控制 (方向键 + 小键盘0)
        case Qt::Key_Up: m_keyPressed2[0] = true; break;
        case Qt::Key_Down: m_keyPressed2[1] = true; break;
        case Qt::Key_Left: m_keyPressed2[2] = true; break;
        case Qt::Key_Right: m_keyPressed2[3] = true; break;
        case Qt::Key_0: m_keyPressed2[4] = true; break;
    }
}

void MainScene::keyReleaseEvent(QKeyEvent *event)
{
    switch(event->key()) {
        // 玩家1控制 (WSAD + 空格)
        case Qt::Key_W: m_keyPressed[0] = false; break;
        case Qt::Key_S: m_keyPressed[1] = false; break;
        case Qt::Key_A: m_keyPressed[2] = false; break;
        case Qt::Key_D: m_keyPressed[3] = false; break;
        case Qt::Key_Space: m_keyPressed[4] = false; break;

        // 玩家2控制 (方向键 + 小键盘0)
        case Qt::Key_Up: m_keyPressed2[0] = false; break;
        case Qt::Key_Down: m_keyPressed2[1] = false; break;
        case Qt::Key_Left: m_keyPressed2[2] = false; break;
        case Qt::Key_Right: m_keyPressed2[3] = false; break;
        case Qt::Key_0: m_keyPressed2[4] = false; break;
    }
}

void MainScene::onReturnToMenu()
{
    // 停止游戏
    m_Timer.stop();
    m_backgroundSound.stop();

    // 发出返回菜单信号
    emit returnToMenuRequested();

    this->hide(); // 先隐藏再删除
      this->deleteLater();
}// 添加重置游戏函数
void MainScene::resetGame(bool isCoopMode)
{
    m_isCoopMode = isCoopMode;
    m_gameOver = false;



    // 重置玩家状态
    m_hero1 = HeroPlane(true);
    m_hero2 = HeroPlane(false);

    m_hero1.m_Free = false;  // 玩家1总是激活
    m_hero2.m_Free = !isCoopMode; // 玩家2只在合作模式激活

    // 重置敌机
    for(int i = 0; i < ENEMY_NUM; i++) {
        m_enemys[i].m_Free = true;
        m_enemys[i].m_Speed = ENEMY_SPEED; // 重置敌机速度
    }

    // 重置子弹
    for(int i = 0; i < BULLET_NUM; i++) {
        m_hero1.getBullets()[i].m_Free = true;
        m_hero1.getBullets()[i].m_Speed = BULLET_SPEED; // 重置子弹速度
        m_hero2.getBullets()[i].m_Free = true;
        m_hero2.getBullets()[i].m_Speed = BULLET_SPEED; // 重置子弹速度
    }

    // 重置爆炸效果
    for(int i = 0; i < BOMB_NUM; i++) {
        m_bombs[i].m_Free = true;
    }

    // 重置其他状态
    m_recorder = 0;

    // 重置定时器间隔
    m_Timer.setInterval(GAME_RATE);

    // 清空键盘状态
    for(int i = 0; i < 5; i++) {
        m_keyPressed[i] = false;
        m_keyPressed2[i] = false;
    }

    // 重置玩家位置
    m_hero1.setPosition(GAME_WIDTH * 0.25 - m_hero1.m_Plane.width() * 0.5, GAME_HEIGHT - m_hero1.m_Plane.height());
    m_hero2.setPosition(GAME_WIDTH * 0.75 - m_hero2.m_Plane.width() * 0.5, GAME_HEIGHT - m_hero2.m_Plane.height());

    //得分
        m_scores[0] = m_scores[1] = 0;
        updateScoreDisplay();


}

void MainScene::onRestartGame() {
    resetGame(m_isCoopMode);
    m_Timer.start();
    m_backgroundSound.play();
}
  //更新得分
void MainScene::updateScore(int player, int delta) {
    m_scores[player] += delta;
    updateScoreDisplay();
}

void MainScene::updateScoreDisplay() {
    if(m_isCoopMode) {
        m_scoreLabel->setText(QString("合作得分: %1").arg(m_scores[0] + m_scores[1]));
    } else {
        m_scoreLabel->setText(QString("得分: %1").arg(m_scores[0]));
    }
}

/*bool MainScene::collisionDetection_bossBullet()
{
    for(int i =0; i<BULLET_NUM;i++)
    {
        if(m_hero.m_Rect.intersects(m_bossplane.m_boss_bullet[i].m_boss_Rect))
        {
            return true;
        }
    }
    return false;
}*/

// 在敌机被击落的地方调用 (假设在碰撞检测处)
void MainScene::onEnemyDestroyed() {
    if(m_isCoopMode) {
        // 双人模式两个玩家都加分
        updateScore(0, 5);
        updateScore(1, 5);
    } else {
        // 单人模式只给玩家1加分
        updateScore(0, 5);
    }
}

void MainScene::handleGameOver()
{
    if(m_gameOver) return;  // 防止重复处理

    m_gameOver = true;
    m_Timer.stop();         // 停止游戏计时器
    m_backgroundSound.stop(); // 停止背景音乐

    // 计算当前得分
    int currentScore = m_isCoopMode ? (m_scores[0] + m_scores[1]) : m_scores[0];

    // 获取最高分
    Menu* menu = qobject_cast<Menu*>(parent());
    int highScore = menu ? menu->getHighScore(!m_isCoopMode) : 0;

    // 创建游戏结束对话框
    QPointer<GameOverDialog> dialog = new GameOverDialog(currentScore, highScore, this);

    // 连接重新开始游戏信号
    connect(dialog, &GameOverDialog::restartGame, this, [this]() {
        resetGame(m_isCoopMode);
        m_Timer.start();
        m_backgroundSound.play();
    });

    // 连接返回菜单信号
    connect(dialog, &GameOverDialog::returnToMenu, this, &MainScene::onReturnToMenu);

    // 显示对话框
    dialog->exec();

    // 清理对话框
    delete dialog;
}


void MainScene::collisionDetection_Prop()
{
    for(int i=0;i<PROP_NUM;i++)
    {
        if(m_props[i].m_Free)
        {
            continue;
        }
        if(m_hero1.m_Rect.intersects(m_props[i].m_Rect) && !m_hero1.m_Free)
                {
                    m_hero1.activateExtraBullets(PROP_DURATION_TIME);
                    m_props[i].m_Free = true;
                    continue;  // 道具已被拾取，检查下一个
                }

                if(m_isCoopMode && !m_hero2.m_Free && m_hero2.m_Rect.intersects(m_props[i].m_Rect))
                {
                    m_hero2.activateExtraBullets(PROP_DURATION_TIME);
                    m_props[i].m_Free = true;
                }
        break;
    }
}
void MainScene::createProp(int x, int y)
{
    for (int i = 0; i < PROP_NUM; i++) {
            if (m_props[i].m_Free) {

                // 设置道具位置（敌机被击败的位置）
                m_props[i].m_X = x;
                m_props[i].m_Y = y;
                m_props[i].m_Rect.moveTo(x, y);

                // 激活道具（标记为非空闲）
                if(rand()%10==1)
                {
                    m_props[i].m_Free = false;
                }
                break;
            }
        }
}
void MainScene::checkBossAppear()
{
    if((m_scores[0]>=100||m_scores[1]>=100) && flag == 0)
    {
        m_bossplane.m_boss_Free=false;
        flag = 1;
    }
}
